using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//this script is responsible for changing a players planet when entering a atmosphere
public class GravitationManager : MonoBehaviour
{
    private List<GameObject>    _gameObjectsTractionable = new List<GameObject>();

	void Start ()
	{

	}

    void Awake()
    {
		
    }

	void Update ()
	{

	}

    void FixedUpdate()
    {
        foreach (GameObject obj in _gameObjectsTractionable)
        {
            Vector3 _objToPlanet = gameObject.transform.parent.transform.position - obj.transform.position;
            Vector3 _gravity = (_objToPlanet.normalized / (_objToPlanet.magnitude * _objToPlanet.magnitude)) * gameObject.transform.parent.rigidbody.mass * obj.rigidbody.mass;
            obj.rigidbody.AddForce(_gravity);
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.GetComponent<Tractionable>() != null)
            _gameObjectsTractionable.Add(other.gameObject);
        

    }

    void OnTriggerStay(Collider other)
    {
        Character _char = null;
        if ((_char = other.GetComponent<Character>()) != null)
            _char._currentPlanet = gameObject.transform.parent.transform;
    }

    void OnTriggerExit(Collider other)
    {
        if (other.GetComponent<Tractionable>() != null)
            _gameObjectsTractionable.Remove(other.gameObject);
    }
}
